<?php


namespace app\common\service\companyIndustry\realtime;


use app\common\service\BaseService;
use app\common\service\FormulaService;
use app\common\service\MongoService;

class RealtimeEmissionService extends BaseService
{
    // +----------------------------------------------------------------------
    // | （九）获取实时企业 小时排放量
    // +----------------------------------------------------------------------
    /**
     * +----------------------------------------------------------
     * 9.1 所有公司 - 所有排口 - 排放量
     * 获取实时企业 - 排口 - 脱硫小时平均排放量 - 平均值(默认前七天)
     * +----------------------------------------------------------
     * @param string $endDay
     * @param int $daysNumber
     * @param string $emissionUnit
     * +----------------------------------------------------------
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getRealtimeAllCompanyDrainEmissionMinToHourAvg($endDay = 'yesterday', $daysNumber = 7, $emissionUnit = 'kg')
    {
        $sulfur = new SulfurEmissionService();
        $nitre  = new NitreEmissionService();
        //脱硫
        $sulfurData = $sulfur->getRealtimeAllCompanyDrainEmissionSulfurMinToHourAvg($endDay, $daysNumber, $emissionUnit);
        //脱硝
        $nitreData = $nitre->getRealtimeAllCompanyDrainEmissionNitreMinToHourAvg($endDay, $daysNumber, $emissionUnit);
        //根据相同的键值合并
        $newData = [];
        foreach ($sulfurData as $k => $v) {
            foreach ($nitreData as $k2 => $v2) {
                if ($k == $k2) {
                    $newData[$k] = array_merge($v, $v2);
                }
            }
        }
        if (!$newData) {
            exception('获取小时数据失败');
        }
        return $newData;
    }

    /**
     * +----------------------------------------------------------
     * 9.2 所有公司 - 排放量
     * 获取实时企业 - 排口 - 脱硫小时平均排放量 - 平均值(默认前七天)
     * +----------------------------------------------------------
     * @param string $endDay
     * @param int $daysNumber
     * @param string $emissionUnit
     * +----------------------------------------------------------
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getRealtimeAllCompanyEmissionMinToHourAvg($endDay = 'yesterday', $daysNumber = 7, $emissionUnit = 'kg')
    {
        $sulfur = new SulfurEmissionService();
        $nitre  = new NitreEmissionService();
        //脱硫
        $sulfurData = $sulfur->getRealtimeAllCompanyEmissionSulfurMinToHourAvg($endDay, $daysNumber, $emissionUnit);
        //脱硝
        $nitreData = $nitre->getRealtimeAllCompanyEmissionNitreMinToHourAvg($endDay, $daysNumber, $emissionUnit);
        //根据相同的键值合并
        $newData = [];
        foreach ($sulfurData as $k => $v) {
            foreach ($nitreData as $k2 => $v2) {
                if ($k == $k2) {
                    $newData[$k] = array_merge($v, $v2);
                }
            }
        }
        if (!$newData) {
            exception('获取小时数据失败');
        }
        return $newData;
    }

    /**
     * +----------------------------------------------------------
     * 9.3 单个公司 - 排放量
     * 获取实时企业 - 排口 - 脱硫小时平均排放量 - 平均值(默认前七天)
     * +----------------------------------------------------------
     * @param int $companyId 公司ID
     * @param string $endDay
     * @param int $daysNumber 天数
     * @param string $emissionUnit 单位
     * @param bool $everyDay 是否输出每天
     * +----------------------------------------------------------
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getRealtimeSingleCompanyEmissionMinToHourAvg($companyId, $endDay = 'yesterday', $daysNumber = 7, $emissionUnit = 'kg', $everyDay = false)
    {
        $sulfur = new SulfurEmissionService();
        $nitre  = new NitreEmissionService();
        //脱硫
        $sulfurData = $sulfur->getRealtimeSingleCompanyEmissionSulfurMinToHourAvg((int)$companyId, $endDay, $daysNumber, $emissionUnit, $everyDay);
        //脱硝
        $nitreData = $nitre->getRealtimeSingleCompanyEmissionNitreMinToHourAvg((int)$companyId, $endDay, $daysNumber, $emissionUnit, $everyDay);
        //根据相同的键值合并
        $newData = [];
        foreach ($sulfurData as $k => $v) {
            foreach ($nitreData as $k2 => $v2) {
                if ($k == $k2) {
                    $newData[$k] = array_merge($v, $v2);
                }
            }
        }

        if (!$newData) {
            exception('获取小时数据失败');
        }
        if ($everyDay) {
            $publishTimeArr = array_column($newData, 'publish_time');
            array_multisort($publishTimeArr, SORT_ASC, $newData);
            return $newData;
        } else {
            return $newData[0];
        }

    }

    /**
     * +----------------------------------------------------------
     * 9.3 单个公司 - 排放量
     * 获取实时企业 - 排口 - 脱硫小时平均排放量 - 平均值(默认前七天)
     * +----------------------------------------------------------
     * @param int $companyId 公司ID
     * @param string $endDay
     * @param int $daysNumber 天数
     * @param string $emissionUnit 单位
     * +----------------------------------------------------------
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getRealtimeSingleCompanyDrainEmissionMinToHourAvg($companyId, $endDay = 'yesterday', $daysNumber = 7, $emissionUnit = 'kg')
    {
        $sulfur = new SulfurEmissionService();
        $nitre  = new NitreEmissionService();
        //脱硫
        $sulfurData = $sulfur->getRealtimeSingleCompanyDrainEmissionSulfurMinToHourAvg((int)$companyId, $endDay, $daysNumber, $emissionUnit);
        //脱硝
        $nitreData = $nitre->getRealtimeSingleCompanyDrainEmissionNitreMinToHourAvg((int)$companyId, $endDay, $daysNumber, $emissionUnit);
        //根据相同的键值合并
        $newData = [];
        foreach ($sulfurData as $k => $v) {
            foreach ($nitreData as $k2 => $v2) {
                if ((int)$v['drain_id'] == (int)$v2['drain_id']) {
                    $newData[$k] = array_merge($v, $v2);
                }
            }
        }
        $newData = array_values($newData);
        if (!$newData) {
            exception('获取小时数据失败');
        }
        $newData = field_as_key($newData, 'drain_id');
        return $newData;
    }

    /**
     * 获取实时企业排口排放量
     * @param $endDay
     * @param $daysNumber
     * @param $emissionUnit
     * @return mixed
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei
     */
    public function getRealtimeDrainMinToHourAvg($endDay, $daysNumber, $emissionUnit)
    {
        $sulfur = new SulfurEmissionService();
        $nitre  = new NitreEmissionService();
        //step1:实时企业
        //脱硫
        $sulfurData = $sulfur->getRealtimeAllCompanyDrainEmissionSulfurMinToHourAvg($endDay, $daysNumber, $emissionUnit);
        //脱硝
        $nitreData = $nitre->getRealtimeAllCompanyDrainEmissionNitreMinToHourAvg($endDay, $daysNumber, $emissionUnit);
        //根据相同的键值合并
        $companyRealtime = [];
        foreach ($sulfurData as $k => $v) {
            foreach ($nitreData as $k2 => $v2) {
                if ($k == $k2) {
                    $companyRealtime[$k] = array_merge($v, $v2);
                }
            }
        }
        return $companyRealtime;
    }

    /**
     * 获取所有企业排放量分钟转小时
     * @param $endDay
     * @param $daysNumber
     * @param $emissionUnit
     * @param $industryId
     * @param $everyDay
     * @param bool $supplement
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei
     */
    public function getRealtimeAllCompanyEmissionsMinToHourAvg($endDay, $daysNumber, $emissionUnit, $industryId, $everyDay, $supplement = false)
    {
        $sulfur = new SulfurEmissionService();
        $nitre  = new NitreEmissionService();
        //step2:实时企业
        //脱硫
        $sulfurData = $sulfur->getRealtimeAllCompanyEmissionSulfurMinToHourAvg($endDay, $daysNumber, $emissionUnit, $industryId, $everyDay);
        //脱硝
        $nitreData = $nitre->getRealtimeAllCompanyEmissionNitreMinToHourAvg($endDay, $daysNumber, $emissionUnit, $industryId, $everyDay);
        //根据相同的键值合并
        $companyRealtime = [];
        if ($everyDay == false) { //有日期
            foreach ($sulfurData as $k => $v) {
                foreach ($nitreData as $k2 => $v2) {
                    if ($v['industry_id'] == $v2['industry_id']) {
                        $companyRealtime[$k] = array_merge($v, $v2);
                    }
                }
            }
        } else { //无日期
            foreach ($sulfurData as $k => $v) {
                foreach ($nitreData as $k2 => $v2) {
                    if ($v['industry_id'] == $v2['industry_id'] && $v['publish_time'] == $v2['publish_time']) {
                        $companyRealtime[$k] = array_merge($v, $v2);
                    }
                }
            }
            array_multisort(array_column($companyRealtime, 'publish_time'), SORT_ASC, $companyRealtime);
        }

        //step3:是否显示合并补数
        if ($supplement == false) {
            foreach ($companyRealtime as $k => $v) {
                $companyRealtime[$k]['so2_emissions'] = $v['so2_emissions_supplement'];
                $companyRealtime[$k]['nox_emissions'] = $v['nox_emissions_supplement'];
                $companyRealtime[$k]['tsp_emissions'] = $v['tsp_emissions_supplement'];
                unset($companyRealtime[$k]['so2_emissions_supplement']);
                unset($companyRealtime[$k]['nox_emissions_supplement']);
                unset($companyRealtime[$k]['tsp_emissions_supplement']);
            }
        }
        return $companyRealtime;
    }

    /**
     * 实时每个行业排放量总和
     * @author hongwei 2019-07-29
     * @param $playNumber
     * @param string $type
     * @return array
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function everyIndustry($playNumber)
    {
        $sulfur           = new SulfurEmissionService();
        $so2TotalEmission = $sulfur->beforeIndustryEvery($playNumber);
        $nitre            = new NitreEmissionService();
        $no2TotalEmission = $nitre->beforeIndustryEvery($playNumber);
        $realEmission     = [];
        foreach ($so2TotalEmission as $k => $v) {
            $realEmission[$k]['industry_id'] = $v['industry_id'];
            foreach ($no2TotalEmission as $k2 => $v2) {
                if ((int)$v['industry_id'] == (int)$v2['industry_id']) {
                    $realEmission[$k]['so2_emissions'] = $v['so2_emissions'];
                    $realEmission[$k]['no2_emissions'] = $v2['no2_emissions'];
                }
            }
        }
        if (!$realEmission) {
            return false;
        }
        return $realEmission;
    }
}